//////////////////////////////////////////////////////////////////////////////////
//	 Copyright 2011 by Yosef Grabivker, ryuho@homenetworksoft.com
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//////////////////////////////////////////////////////////////////////////////////

#include <wx/wx.h>
#include "../common/MarkupSTL.h"
#include "logicmanager.h"
#include "wirelesswlanprocinthw.h"

CWirelessWLANProcIntHW::CWirelessWLANProcIntHW(CLinkGuard& link):
	CWirelessHW(link)
{
}

CWirelessWLANProcIntHW::~CWirelessWLANProcIntHW(void)
{
}


//# cat /proc/wlan/mgmt/staTable
//Stations table
//Number of stations not in the IDLE state:1
//Total number of stations:1
//Number of Authorized Stations:0
//Number of Authorized Stations:1
//Ind        Mac          State       AcRate RateAcx PBCC Priv TIoui WME Chan Band Type
//00 D8-5D-4C-92-AF-02    AUTHORIZED    0x6c  0x1000    0    1     0   0    6 G   STA
std::string CWirelessWLANProcIntHW::GetStats(std::string& strRawData)
{
	wxString strCMD;
	RTSTRING strData;
	long lVal(0);

	CMarkupSTL	doc;
	doc.AddElem( WRLS_XML_DOC_TAG );

	//assoclist
	strData = m_link->ReadFile( CMD_WLAN_MGMT_STAT );

	strRawData = strData;

	if ( !strData.empty() )
	{
		PROCTABLEWITHHEADER table = CLogicManager::ParseTableWithHeaders( strData, 6);

		if (!table.headers.empty() && !table.rows.empty() )
		{
			doc.AddChildElem( WRLS_XML_TAG_ASSOC_LST );
			doc.IntoElem();

			for (size_t i=0;i<table.rows.size();i++)
			{
				wxString strMAC = CLogicManager::GeValFromTable(table, "Mac", i,0);
				strMAC.Replace(wxT("-"), wxT(":"));

				wxString strState = CLogicManager::GeValFromTable(table, "State", i,0);

				doc.AddChildElem( WRLS_XML_TAG_ENTRY,  (const char*)strMAC.ToAscii() );
				doc.AddChildAttrib( WRLS_XML_ATTR_CLNT_STATE,  (const char*)strState.ToAscii() );

				/*
				//rx is packet counter
				int nRX =  atol( CLogicManager::GeValFromTable(table, "Rx", i,0).ToAscii() );
				int nNRXdb=10*log10((double)nRX);
				doc.AddChildAttrib( WRLS_XML_ATTR_CLNT_SIGN_STRNGTH, nNRXdb );
				*/

				long nStrength(0);
				wxString strACX = CLogicManager::GeValFromTable(table, "RateAcx", i,0);
				strACX.ToLong(&nStrength, 16);
				unsigned char btRate = GetStationActiveRate(nStrength);

				doc.AddChildAttrib( WRLS_XML_ATTR_CLNT_SIGN_STRNGTH, (int) btRate  );
			}

			doc.OutOfElem();
		}
	}

	return doc.GetDoc();
}

std::string CWirelessWLANProcIntHW::GetCaps()
{
	CMarkupSTL	doc;
	doc.AddElem( WIRELESS_CAPS );
	doc.AddAttrib( GLOBAL_NAME_ATR,		"AR7" );

	return doc.GetDoc();
}

////////////////////////////////////////////////////////////////////////////////


unsigned char CWirelessWLANProcIntHW::GetStationActiveRate(unsigned short rateAcx)
{
	unsigned char rate=0;

	switch (rateAcx & ACX_RATE_MASK)
	{
		case ACX_RATE_1:
			rate = DOT11_RATE_1;
		break;

		case ACX_RATE_2:
			rate = DOT11_RATE_2;
		break;

		case ACX_RATE_5_5:
			rate = DOT11_RATE_5_5;
		break;

		case ACX_RATE_6:
			rate = DOT11_RATE_6;
		break;

		case ACX_RATE_9:
			rate = DOT11_RATE_9;
		break;

		case ACX_RATE_11:
			rate = DOT11_RATE_11;
		break;

		case ACX_RATE_12:
			rate = DOT11_RATE_12;
		break;

		case ACX_RATE_18:
			rate = DOT11_RATE_18;
		break;

		case ACX_RATE_22:
			rate = DOT11_RATE_22;
		break;

		case ACX_RATE_24:
			rate = DOT11_RATE_24;
		break;

		case ACX_RATE_36:
			rate = DOT11_RATE_36;
		break;

		case ACX_RATE_48:
			rate = DOT11_RATE_48;
		break;

		case ACX_RATE_54:
			rate = DOT11_RATE_54;
		break;
	}

	return rate;
}
